package com.dinadi.yidi.ui.activity;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.FeedBackBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.http.interfaces.IntegralChangeInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;

import java.util.Timer;
import java.util.TimerTask;

/**
 * author: JiaYu.He
 * Date: 2016-11-14   用户反馈
 * Time: 16:12
 */
public class FeedBackActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, text_surplus;
    private EditText edit_feedback_content;
    private ImageView btn_send;
    private String userId, token;


    @Override
    protected View getLayout() {
        view = View.inflate(this, R.layout.activity_feedback, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.person_opinion));
        edit_feedback_content = (EditText) view.findViewById(R.id.edit_feedback_content);
        text_surplus = (TextView) view.findViewById(R.id.text_surplus);
        btn_send = (ImageView) view.findViewById(R.id.btn_send);
        /*自动弹出键盘*/
        edit_feedback_content.setFocusableInTouchMode(true);
        edit_feedback_content.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) edit_feedback_content.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(edit_feedback_content, 0);
                           }

                       },
                200);
        edit_feedback_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() < 300) {
                    text_surplus.setText((300 - s.length()) + "");
                } else {
                    text_surplus.setText(0 + "");
                }

            }

        });
    }

    @Override
    protected void initListener() {
        registerClickListener(btn_send);
    }

    @Override
    public void onResume() {
        super.onResume();
        String isRefresh = SharePreferencesHelper.getInstance(FeedBackActivity.this).getString("isRefresh");
        if ("yes".equals(isRefresh)) {
            userId = SharePreferencesHelper.getInstance(FeedBackActivity.this).getString("userId");
            token = SharePreferencesHelper.getInstance(FeedBackActivity.this).getString("token");
            SharePreferencesHelper.getInstance(FeedBackActivity.this).putString("isRefresh", "no");
        }
    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(this).getString("userId");
        token = SharePreferencesHelper.getInstance(this).getString("token");

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                String content = text_surplus.getText().toString();
                if (content.equals("300")) {
                    toast("内容为空不能提交哦~");
                } else {
                    FeedBackBean bean = new FeedBackBean();
                    bean.setUserId(userId);
                    bean.setToken(userId);
                    bean.setFeedBackContent(content);
                    showLoadingDialog(getResources().getString(R.string.dialog_text_send));
                    CommonInterface feedBackInterface = new CommonInterface(FeedBackActivity.this, httpListener);
                    feedBackInterface.getInterface("11005", JSON.toJSONString(bean));
                }
                break;
            default:
                break;
        }

    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                edit_feedback_content.setText("");
                IntegralChangeInterface.IntegralChange(FeedBackActivity.this, userId, token, "7", "1", "反馈成功");
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            toast("反馈失败");
        }
    };

}
